package com.sanji.sjmallmodule.order;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.sanji.sjmallutils.SharePreferenceUtil;
import com.sanji.sjmallutils.Tools;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 支付页面
 **/
public class OrderPayActivity extends BaseActivity implements View.OnClickListener, NoHttpListener {

    @BindView(R.id.common_title_tv)
    TextView commonTitleTv;
    @BindView(R.id.common_back_img)
    ImageView commonBackImg;
    @BindView(R.id.pop_paymeng_offline_cb)
    CheckBox paymengOfflineCb;
    @BindView(R.id.pop_payment_submit)
    Button paymentSubmit;
    @BindView(R.id.pop_payment_coast)
    TextView paymentCoast;
    private String orderId;
    private Double actualAmount;//实付金额
    private String payType = "";
    private SharePreferenceUtil sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_pay);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        sp=myShare;
        commonTitleTv.setText(getString(R.string.title_order_pay));
        commonBackImg.setOnClickListener(this);
        paymentSubmit.setOnClickListener(this);

        orderId = getIntent().getStringExtra("orderId");
        actualAmount = getIntent().getDoubleExtra("actualAmount", 0);
        paymentCoast.setText(getString(R.string.price_title) + actualAmount);

        paymengOfflineCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    payType = "SJ-HDFK";
                } else {
                    payType = "";
                }

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back_img:
                finish();
                break;
            case R.id.pop_payment_submit:
                //确认付款
//                myShare.put(ConfigConts.IsRefresh, true);

                if ("".equals(payType)) {
                    Tools.toastTools(mCtx, getString(R.string.select_pay_method));
                    return;
                }

                try {
                    JSONObject json = new JSONObject();
                    json.put("userId", myShare.getString(ConfigConts.USER_ID, ""));
                    json.put("orderId", orderId);
                    json.put("frpId", payType);
                    json.put("payPwd", "");


                    Request<JSONObject> payRequest = NoHttp.createJsonObjectRequest(ConfigAPI.ORDER_PAY_URL, RequestMethod.POST);
                    payRequest.setDefineRequestBodyForJson(json);
                    NoHttpCallBack.getInstance().add(this, ConfigAPI.ORDER_PAY_URL.hashCode(), payRequest, this, true, true);

                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
        }

    }

    @Override
    public void onSuccessed(int what, Response response) {
        try {
            JSONObject respJson = new JSONObject(response.get().toString());
            String status = respJson.optString("status");
            if (ConfigConts.STATUS_SUCCESS.equals(status)) {
                Tools.toastTools(mCtx, getString(R.string.pay_status_success));
                intent = new Intent(this, PaySuccessMessageActivity.class);
                startActivity(intent);
//                setResult(RESULT_OK, null);
                finishActivity();
            } else {
                String error = respJson.optString("errorMsg");
                Tools.toastTools(mCtx, error);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailed(int what, Response response) {
        Tools.toastTools(mCtx, getString(R.string.request_false));
    }
}
